package Frame;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import Function.FCPlotLibrary;
import VWorldGameStudio.FCPlot;

/**
 * 编辑或新建剧情。
 */
public class FCEditPlot extends JDialog {

	public FCEditPlot(boolean isnew, FCPlotLibrary pll) {
		setTitle((isnew) ? "剧情系统-添加剧情" : "剧情系统-编剧情件");
		setModal(true);
		setBounds(0, 0, 600, 420);
		setLayout(null);
		setLocationRelativeTo(null);
		setResizable(false);

		JLabel a = new JLabel("剧情id");
		a.setBounds(10, 10, 60, 20);
		this.add(a);

		JComboBox<String> b1 = new JComboBox<>();
		JTextField b2 = new JTextField();
		if (isnew) {
			b2.setBounds(70, 10, 130, 20);
			this.add(b2);
		} else {
			b1.setBounds(70, 10, 130, 20);
			for (int i = 0; i < pll.getPlotNum(); i++) {
				b1.addItem(pll.getIdOfNum(i));
			}
			this.add(b1);
		}

		JLabel c = new JLabel("判断条件:如{\"level\"=0,\"mapName\"=\"home2\"} 将FCPlotTouchOffEntity解析");
		c.setBounds(10, 40, 580, 20);
		this.add(c);

		JTextArea c1 = new JTextArea();
		c1.setLineWrap(true);
		c1.setBorder(BorderFactory.createLineBorder(Color.black));
		c1.setBounds(10, 65, 575, 50);
		this.add(c1);

		JLabel d = new JLabel("动作指令:");
		d.setBounds(10, 125, 575, 20);
		this.add(d);

		JTextArea d1 = new JTextArea();
		JScrollPane d2 = new JScrollPane(d1);
		d2.setAutoscrolls(true);
		d2.setBounds(10, 150, 575, 200);
		this.add(d2);

		JButton ok = new JButton("保存");
		ok.setBounds(10, 360, 575, 20);
		this.add(ok);

		b1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				FCPlot pl = pll.getPlotOfId(b1.getSelectedItem().toString());
				c1.setText(pl.getTouchoff());
				Vector<String> as = pl.getActive();
				d1.removeAll();
				d1.setText("");
				for (String a : as)
					d1.append(a);
			}
		});

		ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					String id;
					if (isnew)
						id = b2.getText();
					else
						id = b1.getSelectedItem().toString();
					System.out.println(id);
					if (id != null && id.length() > 4) {
						if (c1.getText() != null && c1.getText().length() > 0) {
							if (d1.getText() != null && d1.getText().length() > 0) {
								Vector<String> as = new Vector<>();
								for (int i = 0; i < d1.getLineCount(); i++) {
									int begin = d1.getLineStartOffset(i);
									int end = d1.getLineEndOffset(i);
									as.add(d1.getText(begin, end - begin));
								}
								pll.registerPlot(isnew, id, c1.getText(), as);
								new FCTip((isnew) ? "创建剧情" : "编辑剧情", "保存成功");
								dispose();
							} else
								new FCTip((isnew) ? "创建剧情" : "编辑剧情", "动作指令未填写");
						} else
							new FCTip((isnew) ? "创建剧情" : "编辑剧情", "判断条件未填写");
					} else
						new FCTip((isnew) ? "创建剧情" : "编辑剧情", "id名字不得少于5个");
				} catch (Exception ee) {
					ee.printStackTrace();
				}
			}
		});

		setVisible(true);
	}
}
